//
//  SettingViewController.m
//  LYouSubscribeApp
//
//  Created by grx on 2018/12/12.
//  Copyright © 2018年 grx. All rights reserved.
//

#import "LYouSettingViewController.h"
#import "LYouBuyMemberController.h"
#import "LYouMyCollectionController.h"


@interface LYouSettingViewController ()

@property(nonatomic,strong) UIButton *backButton;
@property(nonatomic,strong) UIImageView *bgImageView;
@property(nonatomic,strong) UILabel *versionLable;
@property(nonatomic,strong) UILabel *cacheLable;

@end

@implementation LYouSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.backButton];
    [self.backButton setFtCornerdious:35/2 Corners:UIRectCornerTopRight|UIRectCornerBottomRight];
    CGFloat safeBottem = MainWindows.safeAreaInsets.bottom;
    CGFloat hight = 60;
    if (safeBottem>0) {
        hight = 80;
    }
    /** 背景图片 */
    [self.view addSubview:self.bgImageView];
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(hight);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(90);
    }];
    /** 版本号 */
    [self.view addSubview:self.versionLable];
    [self.versionLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgImageView.mas_bottom).offset(5);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(30);
    }];
    /** 设置列表 */
    NSArray *imageArray = @[@"buyMember",@"myCollection",@"autoPlay",@"clearCache",@"Evaluate"];
    NSArray *titleArray = @[@"购买会员",@"我的收藏",@"自动播放",@"清理缓存",@"评价一下"];

    for (int i=0; i<titleArray.count; i++) {
        /** 按钮 */
        UIButton *selectButotn = [UIButton new];
        selectButotn.tag = i+10;
        [self.view addSubview:selectButotn];
        [selectButotn addTarget:self action:@selector(selectButotnClick:) forControlEvents:UIControlEventTouchUpInside];
        [selectButotn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.versionLable).offset(55+i*55);
            make.height.mas_equalTo(50);
        }];
        /** 图标 */
        UIImageView *iconImage = [UIImageView new];
        iconImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArray[i]]];
        [selectButotn addSubview:iconImage];
        [iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(selectButotn).offset(15);
            make.top.equalTo(selectButotn).offset(14);
            make.width.mas_equalTo(25);
            make.height.mas_equalTo(25);
        }];
        /** 标题 */
        UILabel *titleLable = [UILabel new];
        titleLable.text = titleArray[i];
        titleLable.font = LYFont_Semibold(15);
        [selectButotn addSubview:titleLable];
        [titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(selectButotn).offset(60);
            make.top.equalTo(selectButotn).offset(12);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(30);
        }];
        /** 小箭头 */
        UIImageView *arrowImage = [UIImageView new];
        arrowImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"arrow"]];
        [selectButotn addSubview:arrowImage];
        [arrowImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(selectButotn).offset(-15);
            make.top.equalTo(selectButotn).offset(12);
            make.width.mas_equalTo(30);
            make.height.mas_equalTo(30);
        }];
        if (i==2) {
            /** 自动播放开关 */
            UISwitch * swich = [UISwitch new];
            if ([ISAUTOPLAY isEqualToString:@"yes"]) {
                swich.on = NO;
            }else{
                swich.on = YES;
            }
            swich.tintColor = [UIColor lightGrayColor];//边缘
            swich.backgroundColor = [UIColor lightGrayColor];
            swich.layer.cornerRadius = 15;
            swich.layer.masksToBounds = YES;
            // 设置控件开启状态填充色
            swich.onTintColor = [UIColor blackColor];
            // 设置控件关闭状态填充色
            swich.tintColor = [UIColor lightGrayColor];
            [swich addTarget:self action:@selector(swichChange:) forControlEvents:UIControlEventValueChanged];
            [selectButotn addSubview:swich];
            [swich mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(selectButotn).offset(-25);
                make.top.equalTo(selectButotn).offset(12);
                make.width.mas_equalTo(50);
                make.height.mas_equalTo(30);
            }];
        }
        if (i==3) {
            /** 添加显示缓存lable */
            [selectButotn addSubview:self.cacheLable];
            [self.cacheLable mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(selectButotn).offset(-48);
                make.top.equalTo(selectButotn).offset(2);
                make.width.mas_equalTo(100);
                make.height.mas_equalTo(50);
            }];
        }
    }
}

#pragma mark - 选择是否自动播放
-(void)swichChange:(UISwitch *)swich{
    NSUserDefaults *isAutoPlay = [NSUserDefaults standardUserDefaults];
    if(swich.isOn){
        [isAutoPlay setValue:@"no" forKey:@"ISAUTOPLAY"];
    }else{
        [isAutoPlay setValue:@"yes" forKey:@"ISAUTOPLAY"];
    }
}


#pragma mark - 点击列表
-(void)selectButotnClick:(UIButton *)sender{
    if (sender.tag==10) {
        /** 购买会员 */
        LYouBuyMemberController *buyMemVC = [[LYouBuyMemberController alloc]init];
        buyMemVC.isFromSetting = YES;
        [self.navigationController pushViewController:buyMemVC animated:YES];
    }else if (sender.tag==11){
        /** 我的收藏 */
        LYouMyCollectionController *myCollectVC = [[LYouMyCollectionController alloc]init];
        [self.navigationController pushViewController:myCollectVC animated:YES];
    }else if (sender.tag==13){
        /** 清理缓存 */
        [self clearHuanCun];
    }else if (sender.tag==14){
        /** 评价一下 */
        NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",APPSTOREID];
        //打开链接地址
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:^(BOOL success) {
        }];
    }
}

#pragma mark --- 清除缓存
-(void)clearHuanCun{
    /*清理缓存*/
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"是否清除缓存" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"是"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * _Nonnull action) {
                                                              [[CacheManager shared]clearCache];
                                                              //读取缓存大小
                                                              self.cacheLable.text = [NSString stringWithFormat:@"%.2fM",[[CacheManager shared]gaintCache]];
                                                          }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:nil];
    [alertVC addAction:confirmAction];
    [alertVC addAction:cancelAction];
    [self.navigationController presentViewController:alertVC animated:YES completion:nil];
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton new];
        CGFloat safeBottem = MainWindows.safeAreaInsets.bottom;
        CGFloat hight = 28;
        if (safeBottem>0) {
            hight = 48;
        }
        self.backButton.frame = CGRectMake(0, hight, 45, 35);
        [_backButton setImage:[UIImage imageNamed:@"backLeftBg"] forState:UIControlStateNormal];
        [_backButton setImage:[UIImage imageNamed:@"backLeftBg"] forState:UIControlStateHighlighted];
        [_backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [UIImageView new];
        _bgImageView.image = [UIImage imageNamed:@"VperBgImage"];
    }
    return _bgImageView;
}

- (UILabel *)versionLable {
    if (!_versionLable) {
        _versionLable = [UILabel new];
        _versionLable.text = [NSString stringWithFormat:@"Version %@",[self gaintVersion]];
        _versionLable.font = [UIFont systemFontOfSize:16];
        _versionLable.textAlignment = NSTextAlignmentCenter;
        _versionLable.textColor = [UIColor lightGrayColor];
    }
    return _versionLable;
}

#pragma mark - 获取版本号
-(NSString *)gaintVersion{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    return app_Version;
}

- (UILabel *)cacheLable {
    if (!_cacheLable) {
        _cacheLable = [UILabel new];
        float cacheSize = [[CacheManager shared]gaintCache];
        _cacheLable.text = [NSString stringWithFormat:@"%.2fM",cacheSize];
        _cacheLable.font = [UIFont systemFontOfSize:16];
        _cacheLable.textAlignment = NSTextAlignmentRight;
        _cacheLable.textColor = [UIColor lightGrayColor];
    }
    return _cacheLable;
}


-(void)backButtonClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
